package com.five.admin.server.impl;

import com.five.admin.mapper.CategoryMapper;
import com.five.pojo.entity.Category;
import com.five.pojo.vo.CategoryTreeVo;
import com.five.service.CategoryRpcService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(interfaceClass = CategoryRpcService.class)
public class CategoryRpcServiceImpl implements CategoryRpcService {

    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public List<CategoryTreeVo> queryCategoryTree() {
//        List<Category> list = categoryMapper.queryAll();
        List<Category> list = categoryMapper.selectList(null);
        System.err.println(list);
        return list.stream().filter(item -> item.getParentId() == 0).map(item -> {
            return CategoryTreeVo.builder()
                    .id(item.getId())
                    .name(item.getCategoryName())
                    .children(list.stream().filter(category -> category.getParentId() != 0)
                            .filter(category -> category.getParentId().equals(item.getId()))
                            .map(category -> {
                                return CategoryTreeVo.builder()
                                        .id(category.getId())
                                        .name(category.getCategoryName())
                                        .parentId(item.getId())
                                        .build();
                            }).collect(Collectors.toList())).build();
        }).collect(Collectors.toList());
    }
}
